package com.ruoyi.order.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.order.domain.bo.UserOrderBo;
import com.ruoyi.order.domain.vo.UserOrderVo;
import com.ruoyi.order.service.IUserOrderService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户订单
 *
 * @author zhi
 * @date 2024-03-31
 */
@Tag(name = "用户订单")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/userOrder/userOrder")
@SaIgnore
public class UserOrderController extends BaseController {

    private final IUserOrderService iUserOrderService;

    /**
     * 查询用户订单列表
     */
//    @SaCheckPermission("userOrder:userOrder:list")
    @GetMapping("/list")
    public TableDataInfo<UserOrderVo> list(UserOrderBo bo, PageQuery pageQuery) {
        return iUserOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户订单列表
     */
//    @SaCheckPermission("userOrder:userOrder:export")
    @Log(title = "用户订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserOrderBo bo, HttpServletResponse response) {
        List<UserOrderVo> list = iUserOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户订单", UserOrderVo.class, response);
    }

    /**
     * 获取用户订单详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("userOrder:userOrder:query")
    @GetMapping("/info/{id}")
    public R<UserOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return R.ok(iUserOrderService.queryById(id));
    }

    /**
     * 新增用户订单
     */
//    @SaCheckPermission("userOrder:userOrder:add")
//    @Log(title = "用户订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserOrderBo bo) {
        return toAjax(iUserOrderService.insertByBo(bo));
    }

    /**
     * 修改用户订单
     */
//    @SaCheckPermission("userOrder:userOrder:edit")
    @Log(title = "用户订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserOrderBo bo) {
        return toAjax(iUserOrderService.updateByBo(bo));
    }

    /**
     * 删除用户订单
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("userOrder:userOrder:remove")
    @Log(title = "用户订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iUserOrderService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
